package com.itheima.reggie.util;


import com.itheima.reggie.constant.*;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

@Data
@Component
public class AliyunSmsUtil {
    @Value("${"+CommonConstant.SMS_HOST+"}")
    private String host;
    @Value("${"+CommonConstant.SMS_PATH+"}")
    private String path;
    @Value("${"+CommonConstant.SMS_METHOD+"}")
    private String method;
    @Value("${"+CommonConstant.SMS_APPCODE+"}")
    private String appcode;
    @Value("${"+CommonConstant.SMS_SMSSIGNID+"}")
    private String smsSignId;
    @Value("${"+CommonConstant.SMS_TEMPLATEID+"}")
    private String templateId;

    public void sendSms(String mobile,String code){
        //String appcode = "1b63851623ab422285aba397b81a29af";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", mobile);
        querys.put("param", "**code**:"+code+",**minute**:5");
        querys.put("smsSignId", smsSignId);
        querys.put("templateId", templateId);
        Map<String, String> bodys = new HashMap<String, String>();


        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception{
        String appcode = "1b63851623ab422285aba397b81a29af";

        System.out.println(new String(appcode.getBytes(), "UTF-8"));
    }
}
